package com.example.basic.demo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.core.widget.NestedScrollView

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val scroll = findViewById<NestedScrollView>(R.id.scroll)
    val container = findViewById<LinearLayout>(R.id.container)
    for (i in 0..20) {
      val text = TextView(this)
      text.text = "测试 $i"
      text.gravity = Gravity.CENTER
      text.setBackgroundColor(if (i % 2 == 0) 0x4000ff00 else 0x400000ff)
      val lp = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        50 * 3
      )
      lp.topMargin = 10 * 3
      container.addView(text, lp)
    }

    scroll.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener {
        v, scrollX, scrollY, oldScrollX, oldScrollY ->
      println("onScrollChange y=$scrollY, old=$oldScrollY")
    })

    container.post {
      ViewCompat.offsetTopAndBottom(scroll, 70 * 3)
    }
  }
}